﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Happy.Event.Offline
{
    /// <summary>
    /// 持久化事件拦截器，拦截正在发布的事件，并将事件存储到<see cref="IOfflineEventQueue"/>中。
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public sealed class PersistableAttribute : EventInterceptorAttribute
    {
        /// <inheritdoc />
        public PersistableAttribute(int order)
            : base(order)
        {
        }

        /// <inheritdoc />
        public override void Intercept(IEventPublishContext context)
        {
            context.ExecuteNext();

            var offlineEvent = OfflineEvent.Create(context.Event);

            context
                .Publisher
                .GetService<IOfflineEventQueue>()
                .Send(offlineEvent);
        }
    }
}
